package com.weixing.mall.provider.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.core.support.auth.AuthUtil;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.provider.api.model.vo.MemberRpcVo;
import com.weixing.mall.provider.api.service.MemberServiceRpc;
import com.weixing.mall.provider.mapper.OrderExpressMapper;
import com.weixing.mall.provider.mapper.OrderGoodsMapper;
import com.weixing.mall.provider.mapper.OrderMapper;
import com.weixing.mall.provider.model.query.FrontShipperOrderSearchQuery;
import com.weixing.mall.provider.model.vo.OrderDetilVo;
import com.weixing.mall.provider.model.vo.OrderExpressVo;
import com.weixing.mall.provider.model.vo.ShipperOrderDetailsVo;
import com.weixing.mall.util.CommUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ceshi
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2020/1/1414:16
 */
@Service
public class FrontShipperOrderServerImpl {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderGoodsMapper orderGoodsMapper;

    @Autowired
    private OrderExpressMapper   orderExpressMapper;

    @Autowired
    private MemberServiceRpc memberServiceRpc;

    /**
     * 发货部和APP仓库前端使用订单详情功能
     * @param entity
     * @return
     */
    public Map<String,Object> selectShipperOrder(FrontShipperOrderSearchQuery entity) {
       SysAuth sysExt = AuthUtil.getSysExt();
       Map<String,Object>  result=new HashMap<String, Object>();
        if(null!=entity.getOrderId()){
            ShipperOrderDetailsVo tmp=   orderGoodsMapper.shipperOrderDetailsVo(entity);
            if(null==tmp){
                return  result;
            }
            //敏感数据展示控制
            tmp.setContactPhone(sysExt.isSensitive() ? CommUtil.coverMobile(tmp.getContactPhone()) : tmp.getContactPhone());
            Long  orderId=tmp.getOrderId();
            //获取订单详情进行
            OrderDetilVo ordervo=orderMapper.selectOrderDetails(orderId);
            ordervo.setContactPhone(sysExt.isSensitive() ? CommUtil.coverMobile(ordervo.getContactPhone()) : ordervo.getContactPhone());
            OrderExpressVo expressVo=orderExpressMapper.selectByOrderId(orderId);
            //获取会员联系方式
            Result<MemberRpcVo> memberRpcVoResult = memberServiceRpc.selectById(ordervo.getBuyerId());
            if(memberRpcVoResult.isSuccess() && memberRpcVoResult.getData() != null){
               String memberMobile = memberRpcVoResult.getData().getMobile();
                ordervo.setContactPhone(memberMobile);
            }
            if(ObjectUtil.isNotEmpty(ordervo)){
                //敏感数据展示控制
                ordervo.setContactPhone(sysExt.isSensitive() ? CommUtil.coverMobile(ordervo.getContactPhone()) : ordervo.getContactPhone());
            }
            if(ObjectUtil.isNotEmpty(expressVo)){
                //敏感数据展示控制
                expressVo.setContactPhone(sysExt.isSensitive() ? CommUtil.coverMobile(expressVo.getContactPhone()) : expressVo.getContactPhone());
            }
            result.put("shippperOrderVo",tmp);
            result.put("ordervo",ordervo);
            result.put("expressVo",expressVo);
        }

        return result;
    }
}
